//
//  BRNotificationManager.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/9.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRNotificationManager.h"

#define kIsSetNotification @"kIsSetNotification"



@implementation BRNotificationManager

+ (void)subNoti:(NSString *)notiName status:(void(^)(BOOL isSucc))status {
    if (![[self class] isEnableNoti]) {
        status(NO);
        NSLog(@"通知订阅 -> 已关闭通知接收，不订阅 \n");
        return;
    }
    [[FIRMessaging messaging] subscribeToTopic:notiName completion:^(NSError * _Nullable error) {
        if (error) {
            status(NO);
            NSLog(@"通知订阅 失败 -> %@ \n",error);
        }else {
            status(YES);
            NSLog(@"通知订阅 成功 -> %@ <- \n",notiName);
        }
        
    }];
    
    
}

+ (void)unsubNoti:(NSString *)notiName status:(void(^)(BOOL isSucc))status {
    
    [[FIRMessaging messaging] unsubscribeFromTopic:notiName completion:^(NSError * _Nullable error) {
        if (error) {
            status(NO);
            NSLog(@"取消订阅 失败 -> %@ \n",error);
        }else {
            status(YES);
            NSLog(@"取消订阅 成功 -> %@ <- \n",notiName);
        }
    }];
}


+ (void)subDeviceNoti {
    
    [[self class] subNoti:kAllDeviceNotiKey status:^(BOOL isSucc) {
            
    }];
}

+ (void)subAllNoti {
    [[self class] subDeviceNoti];
    
}


+ (BOOL)isEnableNoti {
    BOOL enableNoti = YES;
    if ([USER_DEFAULT objectForKey:kIsSetNotification])
    {
      enableNoti = [USER_DEFAULT boolForKey:kIsSetNotification];
    }
    else {
        [USER_DEFAULT setBool:YES forKey:kIsSetNotification];
        [USER_DEFAULT synchronize];
        enableNoti = YES;
    }
    return enableNoti;
}

+ (void)setNotification:(BOOL)isEnable {
    [USER_DEFAULT setBool:isEnable forKey:kIsSetNotification];
    [USER_DEFAULT synchronize];
}

@end
